package cn.edu.zust.lxy.wangke_android;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.edu.zust.lxy.wangke_android.util.HttpUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryBooksActivity extends AppCompatActivity {

    private ListView lv_queryBooks;
    private String book_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_books);

        Intent intent = getIntent();
        book_title = intent.getStringExtra("book_title");

        lv_queryBooks = findViewById(R.id.lv_queryBooks);

    }

    @Override
    protected void onStart() {
        super.onStart();
        final List<Map<String, String>> data = new ArrayList<>();
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(HttpUtil.baseurl + "/book/queryBooks" + "?book_title="+book_title)
                            .get()
                            .build();
                    Response response = null;
                    response = client.newCall(request).execute();
                    String result = response.body().string();
                    JSONObject json = new JSONObject(result);
                    JSONArray jsonArr = json.getJSONArray("data");
                    for (int i = 0; i < jsonArr.length(); i++) {
                        JSONObject obj = jsonArr.getJSONObject(i);
                        String book_title = obj.getString("book_title");
                        String book_author = obj.getString("book_author");
                        String book_publish = obj.getString("book_publish");
                        String book_profile = obj.getString("book_profile");
                        String book_appointment = Integer.valueOf(obj.getInt("book_appointment")).toString();
                        String book_borrow = Integer.valueOf(obj.getInt("book_borrow")).toString();
                        HashMap<String, String> map = new HashMap<>();
                        map.put("book_title", book_title);
                        map.put("book_author", book_author);
                        map.put("book_publish", book_publish);
                        map.put("book_profile", book_profile);
                        map.put("book_appointment", book_appointment);
                        map.put("book_borrow", book_borrow);
                        data.add(map);
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
        String[] from = {"book_title","book_author","book_publish"};
        int[] to = {R.id.book_title, R.id.book_author, R.id.book_publish};
        SimpleAdapter adapter = new SimpleAdapter(QueryBooksActivity.this, data, R.layout.book_item, from, to);
        lv_queryBooks.setAdapter(adapter);
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        lv_queryBooks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, String> map = data.get(position);
                Intent intent = new Intent(QueryBooksActivity.this, DetailActivity.class);
                intent.putExtra("book_title", map.get("book_title"));
                intent.putExtra("book_author", map.get("book_author"));
                intent.putExtra("book_publish", map.get("book_publish"));
                intent.putExtra("book_profile", map.get("book_profile"));
                intent.putExtra("book_appointment", map.get("book_appointment"));
                intent.putExtra("book_borrow", map.get("book_borrow"));
                startActivity(intent);
            }
        });
    }
}